package com.web.servlet.novel;

import com.web.bean.novel.Novel;
import com.web.dao.novel.NovelDaoImpl;
import com.web.result.Result;
import com.web.service.novel.NovelService;
import com.web.service.novel.NovelServiceImpl;
import com.web.utils.DBUtil;
import com.web.utils.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--25-9:02
 * @desc
 */
@WebServlet("/novel/*")
public class NovelServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        String uri = req.getRequestURI().replaceAll("/novel/", "");
        if ("selectNovels".equals(uri)) selectNovels(req, resp);
        if ("selectById".equals(uri)) selectById(req, resp);
        if ("addNovel".equals(uri)) addNovel(req, resp);
        if ("updateNovel".equals(uri)) updateNovel(req, resp);
        if ("deleteNovelById".equals(uri)) deleteNovelById(req, resp);

    }


    private void deleteNovelById(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        try {
            String nid=req.getParameter("nid");
            NovelServiceImpl novelService=new NovelServiceImpl();
            novelService.deleteNovelById(nid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            req.getRequestDispatcher("uploadNovel.jsp").forward(req,resp);
        }
    }

    private void updateNovel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nname=req.getParameter("nname");
        String synopsis=req.getParameter("synopsis");
        String nid=req.getParameter("nid");
        String content=req.getParameter("content");
        FileWriter writer=null;
        DBUtil dbUtil=DBUtil.getInstance();
        Result result=new Result();
        try {
            NovelDaoImpl novelDao=new NovelDaoImpl(dbUtil.getConnection());
            Novel novel=novelDao.selectById(nid);
            String nsrc=novel.getNsrc();
            File file=new File(nsrc);
            if (file.isFile()&&file.exists()){
                file.delete();
            }else {
                throw new Exception(("文件不存在"));
            }
            writer.write(content);
            writer.flush();
            novel.setNname(nname);
            novel.setSynopsis(synopsis);
            novel.setNid(nid);
            NovelServiceImpl novelService=new NovelServiceImpl();
            novelService.updateNovel(novel);
            result.setFlag(true);
            result.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            if (writer!=null){
                try {
                    writer.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            req.getRequestDispatcher("uploadNovel.jsp").forward(req,resp);
        }


    }

    private void addNovel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String nname=req.getParameter("nname");
        String synopsis=req.getParameter("synopsis");
        String nid=req.getParameter("nid");
        String nsrc=req.getParameter("nsrc");
        String uid=req.getParameter("uid");
        String code=req.getParameter("code");
        String imgsrc=req.getParameter("imgsrc");
        Integer ncount=Integer.parseInt(req.getParameter("ncount"));
        String content = req.getParameter("content");
        FileWriter writer=null;
        Result result=new Result();
        DBUtil dbUtil=DBUtil.getInstance();
        try {
            NovelDaoImpl novelDao = new NovelDaoImpl(dbUtil.getConnection());
            Novel novel = novelDao.selectById(nid);
            writer = new FileWriter(nsrc);
            writer.write(content);
            writer.flush();
            novel.setNid(nid);
            novel.setCode(code);
            novel.setNsrc(nsrc);
            novel.setNname(nname);
            novel.setSynopsis(synopsis);
            novel.setImgsrc(imgsrc);
            novel.setNcount(ncount);
            novel.setUid(uid);
            NovelServiceImpl novelService=new NovelServiceImpl();
            novelService.addNovel(novel);
            result.setFlag(true);
            result.setMessage("上传成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            dbUtil.close();
            if (writer!=null){
                try {
                    writer.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            req.getRequestDispatcher("uploadNovel.jsp").forward(req,resp);
        }



    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        try {
            String nid=req.getParameter("nid");
            NovelServiceImpl novelService=new NovelServiceImpl();
            novelService.selectById(nid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            req.getRequestDispatcher("uploadNovel.jsp").forward(req,resp);
        }
    }

    private void selectNovels(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result=new Result();
        resp.setCharacterEncoding("UTF-8");
        try {
            NovelServiceImpl novelService=new NovelServiceImpl();
            List<Novel> novels=novelService.selectNovels();
            result.setFlag(true);
            result.setMessage("获取成功");
            result.setData(novels);
        }catch (Exception e){
            e.printStackTrace();
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }finally {
            JSONWriter.writeJSON(result,resp);
            req.getRequestDispatcher("novelRead.jsp").forward(req,resp);
        }


    }
}
